package com.skyhigh.web.webservice.persistence;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

public class TypeHelper {

	public static Type getEquivalent(QueryTypes type){
        Type hbType = null;        
        switch(type){
        	case INTEGER: {
        		hbType=StandardBasicTypes.INTEGER;
        		break;
        	}
        	case STRING: {
        		hbType=StandardBasicTypes.STRING;
        		break;
        	}
			case TIMESTAMP: {
				hbType=StandardBasicTypes.TIMESTAMP;
				break;
			}
        }    
        return hbType;
	}
	
	public static String getTypeName(Parameter param){
		if(param != null){	
			if(param.getType() == StandardBasicTypes.INTEGER){
				return "INTEGER";
			}else if(param.getType() == StandardBasicTypes.LONG){
				return "LONG";
			}else if(param.getType() == StandardBasicTypes.STRING){
				return "STRING";				
			}else if(param.getType() == StandardBasicTypes.SHORT){
				return "SHORT";
			}else if(param.getType() == StandardBasicTypes.BYTE){
				return "BYTE";
			}else if(param.getType() == StandardBasicTypes.FLOAT){
				return "FLOAT";
			}else if(param.getType() == StandardBasicTypes.DOUBLE){
				return "DOUBLE";
			}else if(param.getType() == StandardBasicTypes.CHARACTER){
				return "CHARACTER";
			}else if(param.getType() == StandardBasicTypes.TIME){
				return "TIME";
			}else if(param.getType() == StandardBasicTypes.DATE){
				return "DATE";
			}else if(param.getType() == StandardBasicTypes.TIMESTAMP){
				return "TIMESTAMP";
			}else if(param.getType() == StandardBasicTypes.BOOLEAN){
				return "BOOLEAN";
			}else if(param.getType() == StandardBasicTypes.BIG_DECIMAL){
				return "BIG_DECIMAL";
			}else if(param.getType() == StandardBasicTypes.BIG_INTEGER){
				return "BIG_INTEGER";
			}
		}			
		return "OBJECT";
	}
	
   public static Type inferType(Object value){        
        if(value instanceof Integer)
           return StandardBasicTypes.INTEGER;
        else if(value instanceof Long)
            return StandardBasicTypes.LONG;
        else if(value instanceof Short)
            return StandardBasicTypes.SHORT;
        else if(value instanceof Byte)
            return StandardBasicTypes.BYTE;
        else if(value instanceof Float)
            return StandardBasicTypes.FLOAT;
        else if(value instanceof Double)
            return StandardBasicTypes.DOUBLE;
        else if(value instanceof Character)
            return StandardBasicTypes.CHARACTER;
        else if(value instanceof String)
            return StandardBasicTypes.STRING;
        else if(value instanceof Time)
            return StandardBasicTypes.TIME;
        else if(value instanceof Date)
            return StandardBasicTypes.DATE;
        else if(value instanceof Timestamp)
            return StandardBasicTypes.TIMESTAMP;
        else if(value instanceof Boolean)
            return StandardBasicTypes.BOOLEAN;
        else if(value instanceof BigDecimal)
            return StandardBasicTypes.BIG_DECIMAL;
        else if(value instanceof BigInteger)
            return StandardBasicTypes.BIG_INTEGER;
        else
           return null;
    }	
  /* 
	public static Blob arrayToBlob(Session hbSession, byte[] data){
		return hbSession.getLobHelper().createBlob(data);
	}
*/
	public static byte[] blobToArray(Blob data){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
		  return transformBlob(data, baos);
		} catch (Exception e) {
		}
		return null;		
	}

	private static byte[] transformBlob(Blob fromBlob, ByteArrayOutputStream baos) throws Exception {
	   byte buf[] = new byte[4000];
	   int dataSize;
	   InputStream is = fromBlob.getBinaryStream(); 
	   try {
		 while((dataSize = is.read(buf)) != -1) {
		   baos.write(buf, 0, dataSize);
		 }    
	   } finally {
		 if(is != null) {
		   is.close();
		 }
	   }
	   return baos.toByteArray();
	 }  
}
